/*
 * KI.cpp
 *
 *  Created on: Nov 21, 2013
 *      Author: dima
 */

#include "AI.hpp"
#include "Types.hpp"

#include <stdlib.h>
#include <time.h>
#include <string>



Tanks::
Model::
AI::
AI(Tanks::Model::TankModel * argTank) :
  m_ticks(0),
  m_minDrivingDuration(30),
  m_maxDrivingDuration(100),
  m_currentDrivingDuration(0),
  m_tank(argTank),
  m_startedDriving(-1)
{
  srand(time(NULL));
}



void
Tanks::
Model::
AI::
Tick()
{
  ++m_ticks;

  m_tank->Shoot();
  if(m_startedDriving == -1
  || m_ticks - m_startedDriving >= m_currentDrivingDuration)
  {
    m_currentDrivingDuration = (rand() % (m_maxDrivingDuration - m_minDrivingDuration)) + m_minDrivingDuration;
    m_startedDriving = m_ticks;
    Direction dir = (Direction)(rand() % (DirectionEnd - DirectionLeft) + DirectionLeft);

    switch(dir)
    {
    case DirectionLeft:
      m_tank->SetVelocity(-1, 0);
      break;
    case DirectionUp:
      m_tank->SetVelocity(0, -1);
      break;
    case DirectionRight:
      m_tank->SetVelocity(1, 0);
      break;
    case DirectionDown:
      m_tank->SetVelocity(0, 1);
      break;
    default:
      throw std::string("KI.cpp in Tick: Invalid Direction");
      break;
    }
  }
}


